package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("struts-default")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SubAreaAction extends BaseAction<SubArea> {

	private File file;
	private String fileFileName;

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	@Autowired
	private SubAreaService subAreaService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private FixedAreaService fixedAreaService;

	@Action(value = "subArea_upload")
	public void subAreaUpload() throws Exception {
		// 定义一个集合
		List<SubArea> list = new ArrayList<>();
		// 判断文件类型并解析文件
		Workbook workBook;
		if (fileFileName.endsWith(".xls")) {
			workBook = new HSSFWorkbook(new FileInputStream(file));
		} else {
			workBook = new XSSFWorkbook(new FileInputStream(file));
		}
		// 获取第一sheet并遍历行
		Sheet sheet = workBook.getSheetAt(0);
		for (Row row : sheet) {
			SubArea subArea = new SubArea();
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			subArea.setId(row.getCell(0).getStringCellValue());
			String province = row.getCell(1).getStringCellValue();
			String city = row.getCell(2).getStringCellValue();
			String district = row.getCell(3).getStringCellValue();
			Area area = areaService.findByProvinceAndCityAndDistrict(province, city, district);
			subArea.setArea(area);
			String fixedAreaId = row.getCell(4).getStringCellValue();
			FixedArea fixedArea = fixedAreaService.findOne(fixedAreaId);
			subArea.setFixedArea(fixedArea);
			subArea.setKeyWords(row.getCell(5).getStringCellValue());
			subArea.setStartNum(row.getCell(6).getStringCellValue());
			subArea.setEndNum(row.getCell(7).getStringCellValue());
			subArea.setSingle(Character.valueOf(row.getCell(8).getStringCellValue().charAt(0)));
			subArea.setAssistKeyWords(row.getCell(9).getStringCellValue());
			list.add(subArea);
		}
		subAreaService.save(list);
		workBook.close();
	}

	// 分页展示
	@Action(value = "subArea_showPage")
	public void showPage() {
		Pageable pageable = new PageRequest(page, rows);
		Specification<SubArea> specification = new Specification<SubArea>() {

			@Override
			public Predicate toPredicate(Root<SubArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				return null;
			}
		};
		Page<SubArea> pageData = subAreaService.showPage(specification, pageable);
		writeJson(pageData);
	}

}
